package net.xju3_4.controller;

import net.xju3_4.common.framework.simplemvc.RequestMapping;
import net.xju3_4.common.utils.JSONUtil;
import net.xju3_4.common.utils.PageInfo;
import net.xju3_4.common.utils.Result;
import net.xju3_4.entity.Vehicles;
import net.xju3_4.service.VehiclesService;
import net.xju3_4.service.impl.VehiclesServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;

@RequestMapping("/api/vehicles")
public class VehiclesController {
    VehiclesService vehiclesService = new VehiclesServiceImpl();

    // 添加
    @RequestMapping("/insert.do")
    public void insert(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Vehicles vehicles = new Vehicles();
        vehicles.setPlatenNumber(req.getParameter("platenNumber"));
        vehicles.setBrand(req.getParameter("brand"));
        vehicles.setModel(req.getParameter("model"));
        vehicles.setColor(req.getParameter("color"));
        vehicles.setYear(Integer.parseInt(req.getParameter("year")));
        vehicles.setSeats(Integer.parseInt(req.getParameter("seats")));
        vehicles.setVehicleType(req.getParameter("vehicleType"));
        try {
            vehicles.setPurchaseDate(sdf.parse(req.getParameter("purchaseDate")));
            vehicles.setInsuranceExpiry(sdf.parse(req.getParameter("insuranceExpiry")));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        vehicles.setStatus(req.getParameter("status"));
        vehicles.setDailyRate(Double.parseDouble(req.getParameter("dailyRate")));
        vehicles.setDescription(req.getParameter("description"));
        vehicles.setImages(req.getParameter("images"));

        // vehicles.setCreateTime(LocalDateTime.now());=
        vehiclesService.insert(vehicles);
        JSONUtil.printByJSON(resp, Result.success("添加成功"));
    }
    // 根据id查询用户
    @RequestMapping("/selectOne.do")
    public void selectOne(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        Vehicles vehicles = vehiclesService.selectOne(Integer.parseInt(id));
        JSONUtil.printByJSON(resp, Result.success(vehicles));
    }
    // 删除
    @RequestMapping("/deleted.do")
    public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        vehiclesService.deleteById(Integer.parseInt(id));
        JSONUtil.printByJSON(resp, Result.success("删除成功"));
    }
    // 修改
    @RequestMapping("/update.do")
    public void update(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        Vehicles vehicles = new Vehicles();
        vehicles.setId(Integer.parseInt(req.getParameter("id")));

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        vehicles.setPlatenNumber(req.getParameter("platenNumber"));
        vehicles.setBrand(req.getParameter("brand"));
        vehicles.setModel(req.getParameter("model"));
        vehicles.setColor(req.getParameter("color"));
        vehicles.setYear(Integer.parseInt(req.getParameter("year")));
        vehicles.setSeats(Integer.parseInt(req.getParameter("seats")));
        vehicles.setVehicleType(req.getParameter("vehicleType"));
        try {
            vehicles.setPurchaseDate(sdf.parse(req.getParameter("purchaseDate")));
            vehicles.setInsuranceExpiry(sdf.parse(req.getParameter("insuranceExpiry")));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        vehicles.setStatus(req.getParameter("status"));
        vehicles.setDailyRate(Double.parseDouble(req.getParameter("dailyRate")));
        vehicles.setDescription(req.getParameter("description"));
        vehicles.setImages(req.getParameter("images"));

        // vehicles.setCreateTime(LocalDateTime.now());
        vehiclesService.update(vehicles);
        JSONUtil.printByJSON(resp,Result.success("修改成功"));
    }

    // 条件分页查询
    @RequestMapping("/list.do")
    public void list(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        // 控制层-》业务层-》持久层
        req.setCharacterEncoding("utf-8");
        // 条件查询参数
        String platenNumber = req.getParameter("platenNumber");
        String model = req.getParameter("model");
        // 分页参数  pageNum 第几页   pageSize 每页有多少条
        String pageNoStr = req.getParameter("pageNo");
        String pageSizeStr = req.getParameter("pageSize");
        Integer pageNo = pageNoStr == null ? 1 : Integer.parseInt(pageNoStr);
        Integer pageSize = pageSizeStr == null ? 10 : Integer.parseInt(pageSizeStr);
        PageInfo<Vehicles> pageInfo = vehiclesService.pageList(platenNumber,model,pageNo,pageSize);
        JSONUtil.printByJSON(resp, Result.success(pageInfo));
    }
}
